package com.lingnan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lingnan.entity.po.JobViewRecord;
import com.lingnan.vo.JobViewRecordVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

// JobViewRecordMapper.java
@Mapper
public interface JobViewRecordMapper extends BaseMapper<JobViewRecord> {
    
    /**
     * 检查用户是否已浏览过该职位
     */
    @Select("SELECT COUNT(1) FROM job_view_record WHERE user_id = #{userId} AND job_id = #{jobId}")
    int checkUserViewRecord(@Param("userId") Long userId, @Param("jobId") Long jobId);
    
    /**
     * 获取职位的浏览用户数
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM job_view_record WHERE job_id = #{jobId}")
    int getJobViewUserCount(@Param("jobId") Long jobId);
    
    /**
     * 获取用户的浏览记录分页
     */
    @Select("SELECT jvr.*, jp.title, jp.company_id, c.name as company_name " +
            "FROM job_view_record jvr " +
            "LEFT JOIN job_position jp ON jvr.job_id = jp.id " +
            "LEFT JOIN company c ON jp.company_id = c.id " +
            "WHERE jvr.user_id = #{userId} " +
            "ORDER BY jvr.view_time DESC")
    IPage<JobViewRecordVO> getUserViewRecords(IPage<JobViewRecordVO> page, @Param("userId") Long userId);
}